package 老马书城的加强版;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        File file = new File("D:\\demo.txt");
        if (file.exists()) {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            ois.close();
        }
        ArrayList<Book> list= new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        int choice;
        while (true) {
            // 打印菜单
            System.out.println("*****欢迎来到【周诗雨的书籍城】*****");
            System.out.println("*****1.展示书籍*****");
            System.out.println("*****2.上新书籍*****");
            System.out.println("*****3.下架书籍*****");
            System.out.println("*****4.退出应用*****");

            // 友好的提示
            System.out.println("请输入你想要的功能代码");
            choice = sc.nextInt();

            if (choice == 1) {
                System.out.println("【书城】》》》》》》》1.展示书籍");
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                ArrayList list2 = (ArrayList) ois.readObject();
                if (list2 == null) {
                    System.out.println("当前书库中没有书籍。");
                } else {
                    /*
                    for(int i=0;i<list2.size();i++)
                    {
                        Book Q=(Book)list2.get(i);
                        System.out.println(Q);
                    }
                    */
                    System.out.println(list2);
                }
                ois.close();

            }
            if (choice == 2) {
                System.out.println("【书城】》》》》》》》2.上新书籍");
                // 从键盘输入信息
                System.out.println("请录入书籍编号");
                int id = sc.nextInt();
                System.out.println("请录入书籍名字");
                String name = sc.next();
                System.out.println("请录入书籍作者");
                String autor = sc.next();
                // 每上新一本书籍，就要创建一本书籍对象，初始化
                Book b = new Book(id, name, autor);
                FileOutputStream fos = new FileOutputStream(file);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                list.add(b);
                oos.writeObject(list);
                oos.close();
                fos.close();
            }
            if (choice == 3) {
            System.out.println("【书城】》》》》》》》3.下架书籍");
            // 录入想要删除的图书编号
            System.out.println("请输入您想要下架的书籍编号");
            int delNo = sc.nextInt();
            for (int i = 0; i < list.size(); i++) {
                Book b = list.get(i);
                if (b.getId() == delNo) {
                    list.remove(b);
                    System.out.println("书籍下架成功");
                    break;
                }
            }
            FileOutputStream fosForDel = new FileOutputStream(file);
            ObjectOutputStream oosForDel = new ObjectOutputStream(fosForDel);
            oosForDel.writeObject(list);
            oosForDel.close();
            fosForDel.close();
        }
            if (choice == 4) {
                System.out.println("【书城】》》》》》》》4.退出应用");
                break;
            }
        }
    }
}